<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
  <title>长安整车物流</title>
  <link rel="stylesheet" href="css/base.css">
  <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css">
</head>
<body>
<div style="margin: 16px;">
  正在为您授权，请稍候...
</div>

<script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
<script src="js/api.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
<script>
  var url = decodeURIComponent(getQueryString('url'))
  var code = getQueryString('code')
  getOpenid(code, url, function(data) {
    if (data.detail){
      window.localStorage.setItem('openid', data.detail.openid)
      if (data.detail.driverId) {
        window.location.href = url
        window.localStorage.setItem('driverId', data.detail.driverId)
      } else { // 没有返回driverId，说明没有绑定司机，要跳转到绑定页面
          weui.alert("您尚未注册驾驶员，请前往注册",function () {
              window.location.href = 'reg.html?timeStamp='+new Date().getTime();
          });
      }
    }
  }, function () {
    weui.alert('授权失败，是否重新尝试', function(){
      window.location.href = url
    })
  })
  //获取url后面参数值
  function getQueryString(name) {
    var url = window.location.href
    var reg = new RegExp('(.*/?)&?' + name + '=([^&]*)(&|$)', 'i')
    var r = url.match(reg)
    if (r) { return decodeURI(r[2]) }
    return ''
  }
</script>
</body>
</html>